/*
F * Copyright (C) 2012 Lightbox
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.lightbox.android.photoprocessing;

import java.io.File;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import com.jihox.abookpro.R;
import com.lightbox.android.photoprocessing.utils.BitmapUtils;
import com.lightbox.android.photoprocessing.utils.FileUtils;
import com.lightbox.android.photoprocessing.utils.MediaUtils;

public class PhotoProcessingActivity extends Activity  {
//	private static final String TAG = "PhotoProcessingActivity";
	
	//裁剪
	public static final int PHOTOCROP_CODE = 11020202;
	//调色
	public static final int PHOTOTONING_CODE = 11020203;
	//滤镜
	public static final int PHOTOFILTER_CODE = 11020204;
	//旋转
	public static final int PHOTOREDUCT_CODE = 11020205;
	
	
	private String mOriginalPhotoPath = null;
	private String mCurrentPhotoPath = null;
//	private static String mSavePath = null;
	private Bitmap mBitmap = null;
	private ImageView mImageView = null;
	private ProgressDialog mProgressDialog = null;


	private TextView back_btn = null;
	private TextView save_btn = null;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_photoprocess);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		
		initView();		
		
		Intent intent = getIntent();
		mImageView.setImageBitmap(null);
		mOriginalPhotoPath = intent.getStringExtra("imagepath");
		Log.i("msg", mOriginalPhotoPath);
		loadPhoto(mOriginalPhotoPath);
		mImageView.setImageBitmap(mBitmap);
	}
	
	@SuppressLint("InlinedApi") 
	private void initView() {
		back_btn = (TextView) findViewById(R.id.back_btn);
		back_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mCurrentPhotoPath!=null){
					File file = new File(mCurrentPhotoPath);
					file.delete();
				}
				finish();
			}
		});
		save_btn = (TextView) findViewById(R.id.button_Save);
		save_btn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if(mCurrentPhotoPath!=null){
					showSavingProgressDialog();
					Bitmap bip = BitmapFactory.decodeFile(mCurrentPhotoPath);
					String save_path = FileUtils.savePhoto(bip, mCurrentPhotoPath, false);
					File file = new File(mCurrentPhotoPath);
					file.delete();
					Intent mIntent=new Intent();
					mIntent.putExtra("editImagePath", save_path);
					setResult(RESULT_OK, mIntent);
					hideProgressDialog();
					Log.i("save_path", save_path);
				}
				finish();
				
//				Toast.makeText(PhotoProcessingActivity.this, save_path, Toast.LENGTH_SHORT).show();
				
			}
		});
		mImageView = (ImageView)findViewById(R.id.imageViewPhoto);	

	}	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);				
		
		if(requestCode == PHOTOCROP_CODE && resultCode == RESULT_OK){//裁剪
			mOriginalPhotoPath = mCurrentPhotoPath = data.getStringExtra("cropImagePath");
			Log.i("msg_resultpath", data.toString()+"------------"+mCurrentPhotoPath);
			mImageView.setImageBitmap(null);
			loadPhoto(mCurrentPhotoPath);
			mImageView.setImageBitmap(mBitmap);
		}else if(requestCode == PHOTOTONING_CODE && resultCode == RESULT_OK){//调色
			mOriginalPhotoPath = mCurrentPhotoPath = data.getStringExtra("toningImagePath");
			Log.i("msg_resultpath", data.toString()+"------------"+mCurrentPhotoPath);
			mImageView.setImageBitmap(null);
			loadPhoto(mCurrentPhotoPath);
			mImageView.setImageBitmap(mBitmap);
		}else if(requestCode == PHOTOFILTER_CODE && resultCode == RESULT_OK){//滤镜
			mOriginalPhotoPath = mCurrentPhotoPath = data.getStringExtra("filterImagePath");
			Log.i("msg_resultpath", data.toString()+"------------"+mCurrentPhotoPath);
			mImageView.setImageBitmap(null);
			loadPhoto(mCurrentPhotoPath);
			mImageView.setImageBitmap(mBitmap);
		}else if(requestCode == PHOTOREDUCT_CODE && resultCode == RESULT_OK){//旋转
			mOriginalPhotoPath = mCurrentPhotoPath = data.getStringExtra("reductImagePath");
			Log.i("msg_resultpath", data.toString()+"------------"+mCurrentPhotoPath);
			mImageView.setImageBitmap(null);
			loadPhoto(mCurrentPhotoPath);
			mImageView.setImageBitmap(mBitmap);
		}
	}
	
	
	//裁剪
	public void onCropButtonClick(View v) {
		Intent intent = new Intent(PhotoProcessingActivity.this,PhotoCropActivity.class);
		intent.putExtra("mOriginalPhotoPath", mOriginalPhotoPath);
		startActivityForResult(intent, PHOTOCROP_CODE);
		
	}
	//调色
	public void onToningButtonClick(View v) {
		Intent intent = new Intent(PhotoProcessingActivity.this,PhotoToningActivity.class);
		intent.putExtra("mOriginalPhotoPath", mOriginalPhotoPath);
		startActivityForResult(intent, PHOTOTONING_CODE);
	}
	//滤镜
	public void onFilterButtonClick(View v) {	
		Intent intent = new Intent(PhotoProcessingActivity.this,PhotoFilterActivity.class);
		intent.putExtra("mOriginalPhotoPath", mOriginalPhotoPath);
		startActivityForResult(intent, PHOTOFILTER_CODE);
	}
	//旋转
	public void onRotateButtonClick(View v) {
		Intent intent = new Intent(PhotoProcessingActivity.this,PhotoReductActivity.class);
		intent.putExtra("mOriginalPhotoPath", mOriginalPhotoPath);
		startActivityForResult(intent, PHOTOREDUCT_CODE);
	}
	
		
	private void loadPhoto(String path) {
		DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
		
		if (mBitmap != null) {
			mBitmap.recycle();
		}
		
		mBitmap = BitmapUtils.getSampledBitmap(path, displayMetrics.widthPixels, displayMetrics.heightPixels,false);
		
		if (mBitmap != null && !mBitmap.isMutable()) {
			mBitmap = PhotoProcessing.makeBitmapMutable(mBitmap);
		}
		
		int angle = MediaUtils.getExifOrientation(path);
		mBitmap = PhotoProcessing.rotate(mBitmap, angle);
		

	}	
	
	private void showSavingProgressDialog() {
		String message = "保存中...";
		mProgressDialog = ProgressDialog.show(this, "", message);
	}
	
	private void hideProgressDialog() {
		if (mProgressDialog != null && mProgressDialog.isShowing()) {
			mProgressDialog.dismiss();
		}
	}
}